#include "main.h"

void *pthread_net_read(void * arg)
{
    arg=arg;
    HELMET_MSG recvmsg={0};
    while(1)
    {
        int ret = mysock_read(h_info.sockfd, &recvmsg,sizeof(recvmsg));
        printf("recvmsg.command is %d\r\n",recvmsg.command);
        if(ret <= 0)
        {
            exit(0);
        }
        switch (recvmsg.command)
        {
            case XYD_MSG_ERROR:
                exit(0);//守护进程自动重启程序
                break;
            //case XYD_MSG_VIDEO:
            //    break;
            case XYD_MSG_AUIDO:
                Recv_Audio_data(recvmsg);
                break;
            case XYD_VIDEO_STOP:
                h_info.video_net_status = false;
                break;
            case XYD_VIDEO_START:
                h_info.video_net_status = true;
                break;
            case XYD_AUIDO_START:
                h_info.auido_net_status = true;
                break;
            case XYD_AUIDO_STOP:
                h_info.auido_net_status = false;
                break;
            case XYD_MSG_CLIENT_ERR:
                printf("设备ID非法,请联系管理员!\r\n");
                printf("设备合法后请重启设备!\r\n");
                close(h_info.sockfd);
                pthread_exit(NULL);
                break;
            default:
                break;
        }
    }
}

int Init_MyNetWork(void)
{
    gps_skd = gps_server_connect(); //连接GPS服务器

    int ret =  0;
    int val = 1;
    struct sockaddr_in addr;
    struct sockaddr_in server_addr;

    h_info.sockfd = socket(AF_INET,SOCK_STREAM,0);   
    setsockopt(h_info.sockfd,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(val));
    setsockopt(h_info.sockfd,SOL_SOCKET,SO_REUSEPORT,&val,sizeof(val));

    addr.sin_addr.s_addr = inet_addr(h_info.self_ip);
    addr.sin_port = htons(h_info.self_port);
    addr.sin_family = AF_INET;
    
    ret = bind(h_info.sockfd,(struct sockaddr *)&addr,sizeof(addr));
    if(ret < 0)
    {
        perror("bind");
        printf("绑定IP地址失败！\r\n");
        close(h_info.sockfd);
        return ret;
    }
    printf("绑定IP成功！\r\n");
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    server_addr.sin_port = htons(SERVER_PORT);
    ret = connect(h_info.sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr));
    if(ret < 0)
    {
        perror("connect");
        printf("服务器不在线！\r\n");
        close(h_info.sockfd);
        exit(0);
    }
    printf("服务器连接成功!\r\n");
    sleep(1);
    HELMET_MSG sendmsg={
        .xyd="xyd",
    };
    sendmsg.command = XYD_MSG_CLIENT;
    sendmsg.device_id = h_info.device_id;
    sendmsg.audio_device_id = 123456;
    sendmsg.data_ptr = (uint8_t * )333;
    sendmsg.data_length = 1;
    sendmsg.sum = sum_value(sendmsg);
    printf("sum==%d\r\n",sendmsg.sum);
    printf("sizeof(HELMET_MSG)==%d\r\n",sizeof(HELMET_MSG));
    mysock_write(h_info.sockfd,&sendmsg,sizeof(HELMET_MSG));
    printf("发送校验数据成功！\r\n");

    //客户端消息处理线程
    pthread_create(&h_info.net_pd,NULL,pthread_net_read,NULL);
    pthread_create(&h_info.gps_pd,NULL,pthread_gps_read,NULL);
    pthread_create(&h_info.mpu6050_pd,NULL,pthread_mpu6050_read,NULL);
    pthread_create(&h_info.ctl_pd,NULL,pthread_ctl,NULL);
    return 0;
}

int mysock_read(int sockfd,void * data,int size)
{
    int len = size;
    int ret = 0;
    uint8_t * p = data;
    while(1)
    {
        ret = read(sockfd,p,len);
        if(ret < 0)
        {
            perror("read");
            printf("接受数据失败！\r\n");
            return ret;
        }else if(ret == 0)
        {
            printf("客户离线!\r\n");
            close(sockfd);
            return 0;
        }else 
        {

            len = len - ret;
            p+=ret;
            if(len == 0)
            {
                break;
            }
        }

    }
    return size;
}

int mysock_write(int sockfd,void * data,int size)
{
    int len = size;
    int ret = 0;
    uint8_t * p = data;
    while(1)
    {
        ret = write(sockfd,p,len);
        if(ret < 0)
        {
            perror("write");
            printf("发送数据失败！\r\n");
            return ret;
        }else if(ret == 0)
        {
            printf("客户离线!\r\n");
            close(sockfd);
            return 0;
        }else 
        {

            len = len - ret;
            p+=ret;
            if(len == 0)
            {
                break;
            }
        }

    }
    return size;
}


